package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.IPermissionService;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.PermissionQuery;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") PermissionQuery qo) {
        PageInfo<Permission> pageResult = permissionService.queryForList(qo);
        model.addAttribute("pageResult", pageResult);
        return "permission/list";//跳转到list.jsp页面/WEB-INF/view/permission/list.flt
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {
        System.out.println(permission.getId());
        if (permission.getId() == null) {
            permissionService.insert(permission);
        }
        permissionService.updateByPrimaryKey(permission);
        return "redirect:/permission/list";//跳转到list.jsp页面/WEB-INF/view/permission/list.flt
    }

    @RequestMapping("/delete")
    public String delete(Model model,Long id) {
       permissionService.deleteByPrimaryKey(id);
        return "redirect:/permission/list";//跳转到list.jsp页面/WEB-INF/view/permission/list.flt
    }
    @RequestMapping("/input")
    public String edit(Model model,Long id) {
        if (id!=null){
            Permission permission = permissionService.selectByPrimaryKey(id);
            model.addAttribute("permission",permission);
            System.out.println(permission);
        }
        return "permission/input";//跳转到list.jsp页面/WEB-INF/view/permission/list.flt
}

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        JsonResult result = new JsonResult();
        try {
            permissionService.reload();
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("更新失败");
        }
        return result;
    }
}
